import os
import time
import uuid

from ckeditor_uploader.fields import RichTextUploadingField
from django.contrib.auth.models import User
from django.db import models


def common_save(self):
    self.id = uuid.uuid1()


# 标签
class Label(models.Model):
    class Meta:
        verbose_name = '随笔标签'
        verbose_name_plural = "随笔标签"

    id = models.UUIDField(primary_key=True, editable=False, auto_created=True, default=uuid.uuid4)
    title = models.CharField('标题', max_length=10)

    def __str__(self):
        return self.title


# 图片
def upload_to(instance, filename):
    now_time = time.localtime()
    name = filename
    if name is None:
        name = time.strftime("%HH-%mm-%ss", now_time)
    now_year = time.strftime("%Y", now_time)
    now_month = time.strftime("%m", now_time)
    url = now_year + os.path.sep + now_month + os.path.sep + name
    return url


class Series(models.Model):
    class Meta:
        verbose_name = '系列随笔'
        verbose_name_plural = "系列随笔"
        ordering = ['-update_time']

    id = models.UUIDField(primary_key=True, editable=False, auto_created=True, default=uuid.uuid4)
    series_name = models.CharField(verbose_name='系列名称', db_column='series_name', max_length=30, blank=True)
    series_description = models.TextField(verbose_name='系列描述', db_column='series_description', blank=True)
    create_time = models.DateTimeField(verbose_name='发布时间', db_column='create_time', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', db_column='update_time', auto_now=True)

    def __str__(self):
        return self.series_name


# 随笔
class Essay(models.Model):
    class Meta:
        verbose_name = '随笔'
        verbose_name_plural = "随笔"
        ordering = ['-create_time']

    id = models.UUIDField(primary_key=True, editable=False, auto_created=True, default=uuid.uuid4)
    title = models.CharField('标题', max_length=200)
    summary = models.CharField('简介', max_length=255, blank=True)
    content = models.TextField('内容', blank=True, null=True, default="")
    html_content = models.TextField('渲染页面', blank=True, null="", default="")
    clicks_num = models.IntegerField('点击次数', default=0, blank=True)
    clicks_like = models.IntegerField('点赞次数', default=0, blank=True)
    create_time = models.DateTimeField('发布时间', auto_now_add=True, blank=True)
    update_time = models.DateTimeField('最后更新时间', auto_now=True, blank=True)
    labels = models.ManyToManyField(Label, verbose_name='标签', blank=True)
    creator = models.ForeignKey(User, related_name="creator", on_delete=models.SET_DEFAULT, default=None)
    series = models.ForeignKey(Series, null=True, blank=True, related_name='essay_series', verbose_name='所属系列',
                               on_delete=models.SET_DEFAULT, default=None)
    rich_text = RichTextUploadingField("博客内容", name='rich_text', null=True, blank=True, default="")

    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title


class Picture(models.Model):
    class Meta:
        verbose_name = '随笔图片'
        verbose_name_plural = "随笔图片"
        ordering = ['order', 'create_time']

    id = models.UUIDField(primary_key=True, editable=False, auto_created=True, default=uuid.uuid4)
    picture_name = models.CharField('图片名称', blank=True, max_length=100)
    picture_url = models.ImageField('图片', upload_to=upload_to, blank=False)
    create_time = models.DateTimeField('发布时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)
    order = models.IntegerField('顺序', default=0)
    essay = models.ForeignKey(Essay, verbose_name='所属博客', related_name='essay_picture', on_delete=models.CASCADE)

    def __str__(self):
        return self.picture_name + ":" + str(self.picture_url)

    def __unicode__(self):
        return '%s: %s' % (self.order, self.picture_url)


class Comment(models.Model):
    class Meta:
        verbose_name = '随笔评论'
        verbose_name_plural = "随笔评论"
        ordering = ['create_time']

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        if self.id is None or self.id == '':
            self.id = uuid.uuid1()
        super(Comment, self).save()

    id = models.UUIDField(primary_key=True, editable=False, auto_created=True, default=uuid.uuid4)
    parent_id = models.CharField('父评论ID', db_column='parent_id', max_length=36, blank=True)
    nick_name = models.CharField('昵称', blank=True, max_length=100)
    rich_text = RichTextUploadingField("博客内容", name='rich_text', null=True, blank=True, default="")
    create_time = models.DateTimeField('发布时间', auto_now_add=True)
    update_time = models.DateTimeField('更新时间', auto_now=True)
    essay = models.ForeignKey(Essay, verbose_name='所属博客', related_name='essay_comment', on_delete=models.CASCADE,
                              default="")

    def __str__(self):
        return self.nick_name
